package com.imooc.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MpegVideoMP3 {

	private String ffMpeg;

	public MpegVideoMP3(String ffMpeg) {
		super();
		this.ffMpeg = ffMpeg;
	}

	public void convertor(String inputVideoPath, String inputMp3Path, double seconds, String outPutPath)
			throws Exception {
		List<String> command = new ArrayList<String>();
		command.add(ffMpeg);

		command.add("-i");
		command.add(inputMp3Path);
		command.add("-i");
		command.add(inputVideoPath);
		command.add("-t");
		command.add(String.valueOf(seconds));
		command.add("-y");
		command.add(outPutPath);

		ProcessBuilder processBuilder = new ProcessBuilder(command);
		Process process = processBuilder.start();

		InputStream errorStream = process.getErrorStream();
		InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
		BufferedReader br = new BufferedReader(inputStreamReader);

		String line = "";
		while ((line = br.readLine()) != null) {

		}

		if (br != null) {
			br.close();
		}
		if (inputStreamReader != null) {
			br.close();
		}
		if (errorStream != null) {
			br.close();
		}

	}

	public static void main(String[] args) throws Exception {

		MpegVideoMP3 ffMpegTest = new MpegVideoMP3("C:\\ffmpeg\\bin\\ffmpeg.exe");
		ffMpegTest.convertor("C:\\ffmpeg\\bin\\test.mp4","C:\\ffmpeg\\bin\\bgm.mp3",10.0,
				"C:\\ffmpeg\\bin\\demo.mp4");

	}

}
